package q125_isPalindrome;

public class Solution_1 {
    public static void main(String[] args) {
        Solution_1 s = new Solution_1();
        System.out.println(s.isPalindrome("OP"));
    }
    public boolean isPalindrome(String s) {
        String a = "";
        for (int i = 0; i < s.length(); i++) {
            a += ((s.charAt(i) <= 'Z' && s.charAt(i) >= 'A') || (s.charAt(i) <= 'z' && s.charAt(i) >= 'a') || (s.charAt(i) <= '0' && s.charAt(i) >= '9')) ? s.charAt(i) : "";
        }
        if (a.length() == 0) {
            return true;
        }
        System.out.println(a);
        return judge(a.toLowerCase());
    }

    private boolean judge(String a) {

        if(a.length() % 2 == 0){
            int i = (a.length() - 1)/2;
            int j = i + 1;
            while (i >= 0){
                if(a.charAt(i) != a.charAt(j)){
                    return false;
                }
                i--;
                j++;
            }
        }else{
            int i = (a.length() - 1)/2 - 1;
            int j = (a.length() - 1)/2 + 1;
            while (i >= 0){
                if(a.charAt(i) != a.charAt(j)){
                    return false;
                }
                i--;
                j++;
            }
        }
        return true;
    }
}
